package com.qianniu.napi.admin.service.akka;

import akka.actor.ActorRef;
import akka.actor.Props;
import akka.actor.UntypedActor;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.google.common.base.Strings;
import com.qianniu.napi.admin.entity.*;
import com.qianniu.napi.admin.mapper.StoreStatMapper;
import com.qianniu.napi.admin.service.IBannerService;
import com.qianniu.napi.admin.service.IGoodsService;
import com.qianniu.napi.admin.service.INewsService;
import com.qianniu.napi.admin.service.IStoreService;
import com.qianniu.napi.common.config.Constants;
import com.qianniu.napi.common.util.ImageUtil;
import com.qianniu.napi.common.util.PictureUtil;
import com.qianniu.napi.common.util.SpringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class StoreActor extends UntypedActor {

    private final static Logger logger = LoggerFactory.getLogger(StoreActor.class);
    private final static String rootsite = Constants.IMG_UPLOAD_HOST;
    private final static String rootsitecdn = Constants.IMG_UPLOAD_HOST.replace("//img.", "//imgcdn.");
    private IStoreService storeService = (IStoreService) SpringUtil.getBean("storeServiceImpl");
    private IBannerService bannerService = (IBannerService) SpringUtil.getBean("bannerServiceImpl");
    private IGoodsService goodsService = (IGoodsService) SpringUtil.getBean("goodsServiceImpl");
    private INewsService newsService = (INewsService) SpringUtil.getBean("newsServiceImpl");
    private StoreStatMapper storeStatMapper = (StoreStatMapper) SpringUtil.getBean("storeStatMapper");

    @Override
    public void onReceive(Object message) {

        if (message instanceof String) {
            String msg = message.toString();
            logger.debug(">>>>>> StoreActor receive message:" + msg);

            // 格式 type+uid+toke
            String[] arr = msg.split("@");
            if (arr.length < 2) return;

            String type = arr[0];
            String sid = arr[1];
            if (sid == null || sid.equals("null")) {
                return;
            }

            if (type.equals(Constants.AKKA_MSG_STORE_VIEW)) {
                Store store = storeService.selectById(sid);
                store.setRenqi(store.getRenqi() == null ? 1 : store.getRenqi() + 1);
                store.setViews(store.getViews() == null ? 1 : store.getViews() + 1);
                store.updateById();

                StoreStat storeStat = storeStatMapper.selectById(store.getId());
                if (storeStat == null) {
                    storeStat = new StoreStat();
                    storeStat.setStoreid(store.getId());
                }
                storeStat.setStorename(store.getName());
                storeStat.setStorepic(store.getThumb());
                storeStat.setRenqi(storeStat.getRenqi() + 1);
                storeStat.setViews(storeStat.getViews() + 1);
                storeStat.updateById();

            } else if (type.equals(Constants.AKKA_MSG_STORE_ORDER)) {
                Store store = storeService.selectById(sid);
                store.setRenqi(store.getRenqi() + 5);
                store.updateById();

            } else if (type.equals(Constants.AKKA_MSG_STORE_MODIFY)) {
                yasuoStoreImage(sid);
            } else if (type.equals(Constants.AKKA_MSG_BANNER_MODIFY)) {
                yasuoBannerImage(sid);
            } else if (type.equals(Constants.AKKA_MSG_NEWS_MODIFY)) {
                yasuoNewsImage(sid);
            } else if (type.equals(Constants.AKKA_MSG_GOODS_MODIFY)) {
                yasuoGoodsImage(sid);
            }
        }
    }

    // 压缩门店
    private void yasuoStoreImage(String sid) {

        Store item = storeService.selectOne(new EntityWrapper<Store>()
                .setSqlSelect("id,thumb,pic,photos,xiangqing,zhizhao,xukezheng,latitude,longitude,mpid")
                .eq("id", sid)
        );
        if (item == null) return;

        //燕郊所属商圈联盟
        League league = storeService.getNearbyLm(item.getLatitude(), item.getLongitude(), item.getMpid());
        logger.info("-------------------------------------------------------------yasuoStoreImage league=" + JSON.toJSONString(league));

        if (league != null) {
            Store nstore = new Store();
            nstore.setId(item.getId());
            nstore.setLeagueid(league.getId());
            nstore.updateById();
        }
//        if (!Strings.isNullOrEmpty(item.getThumb())) {
//            String img = item.getPic().replace(rootsite, Constants.PATH_IMAGES);
//            PictureUtil.yasuo(img,200,200);
//        }
        boolean iscdn = false;
        if (!Strings.isNullOrEmpty(item.getPic())) {

            iscdn = item.getPic().contains("//imgcdn.");
            String img = iscdn ? item.getPic().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/") : item.getPic().replace(rootsite, Constants.PATH_IMAGES);
            String imgthumb = img.replace(".", "thumb.");

            try {
                Runtime.getRuntime().exec("cp  " + img + " " + imgthumb);
                logger.info("----------------------------------------------------yasuoStoreImage pic copy ok");

            } catch (Exception e) {
                logger.error("---------Runtime.getRuntime().exec error", e);
            }

            PictureUtil.yasuo(img, 750, 540);

            PictureUtil.yasuo(imgthumb, 200, 200);

            Store nstore = new Store();
            nstore.setThumb(iscdn ? imgthumb.replace(Constants.PATH_IMAGES + "0cdn/", rootsitecdn) : imgthumb.replace(Constants.PATH_IMAGES, rootsite));
            nstore.setId(item.getId());
            nstore.updateById();
        }

        if (!Strings.isNullOrEmpty(item.getPhotos())) {

            String[] photos = iscdn ? item.getPhotos().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getPhotos().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }

        if (!Strings.isNullOrEmpty(item.getXiangqing())) {
            String[] photos = iscdn ? item.getXiangqing().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getXiangqing().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }

        if (!Strings.isNullOrEmpty(item.getZhizhao())) {
            String[] photos = iscdn ? item.getZhizhao().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getZhizhao().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }

        if (!Strings.isNullOrEmpty(item.getXukezheng())) {
            String[] photos = iscdn ? item.getXukezheng().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getXukezheng().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }
    }

    // 压缩轮播图
    private void yasuoBannerImage(String id) {

        Banner item = bannerService.selectOne(new EntityWrapper<Banner>().setSqlSelect("id,pic")
                .eq("id", id));
        if (item == null) return;

        Boolean iscdn = false;
        if (!Strings.isNullOrEmpty(item.getPic())) {
            iscdn = item.getPic().contains("//imgcdn.");
            String img = iscdn ? item.getPic().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/")
                    : item.getPic().replace(rootsite, Constants.PATH_IMAGES);
            PictureUtil.yasuo(img, null, null);
        }
    }

    // 压缩新闻图
    private void yasuoNewsImage(String id) {

        News item = newsService.selectOne(new EntityWrapper<News>().setSqlSelect("id,pic,photos")
                .eq("id", id));
        if (item == null) return;

        Boolean iscdn = false;
        if (!Strings.isNullOrEmpty(item.getPic())) {
            iscdn = item.getPic().contains("//imgcdn.");
            String img = iscdn ? item.getPic().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/")
                    : item.getPic().replace(rootsite, Constants.PATH_IMAGES);
            PictureUtil.yasuo(img, null, null);
        }

        if (!Strings.isNullOrEmpty(item.getPhotos())) {
            String[] photos = iscdn ? item.getPhotos().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getPhotos().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }

    }

    // 压缩商品图
    private void yasuoGoodsImage(String id) {

        Goods item = goodsService.selectOne(new EntityWrapper<Goods>().setSqlSelect("id,thumb,pic,photos,info,shareimg,packinglist,afterservice,introduction,type,mpid")
                .eq("id", id));
        if (item == null) return;
        logger.info("---------------------------------------yasuoGoodsImage goods=" + JSON.toJSONString(item));
//        if (!Strings.isNullOrEmpty(item.getThumb())) {
//            String img = item.getPic().replace(rootsite, Constants.PATH_IMAGES);
//            PictureUtil.yasuo(img,200,200);
//        }
        Boolean iscdn = false;

        if (!Strings.isNullOrEmpty(item.getPic())) {

            iscdn = item.getPic().contains("//imgcdn.");
            logger.info("---------------------------------------yasuoGoodsImage pic");
            String img = iscdn ? item.getPic().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/") : item.getPic().replace(rootsite, Constants.PATH_IMAGES);

            String imgthumb = img.replace(".", "thumb.");
            String imgthumbhd = img.replace(".", "thumbhd.");
            String shareimghd = img.replace(".","shareimghd.");
            try {
                Runtime.getRuntime().exec("cp  " + img + " " + imgthumb);
                Runtime.getRuntime().exec("cp  " + img + " " + imgthumbhd);
                Runtime.getRuntime().exec("cp  " +img+ " "+shareimghd);
                logger.info("-------------------------------------------------------------yasuoGoodsImage pic copy ok");

            } catch (Exception e) {
                logger.error("---------Runtime.getRuntime().exec error", e);
            }

            PictureUtil.yasuo(img, 750, 750);
            PictureUtil.yasuo(imgthumb, 200, 200);
            if (item.getMpid().longValue() == 1) PictureUtil.yasuo(imgthumbhd, 750, 548);
            else PictureUtil.yasuo(imgthumbhd, 750, 420);

            Goods ngoods = new Goods();
            ngoods.setThumb(iscdn ? imgthumb.replace(Constants.PATH_IMAGES + "0cdn/", rootsitecdn) : imgthumb.replace(Constants.PATH_IMAGES, rootsite));
            ngoods.setThumbhd(iscdn ? imgthumbhd.replace(Constants.PATH_IMAGES + "0cdn/", rootsitecdn) : imgthumbhd.replace(Constants.PATH_IMAGES, rootsite));
            if(Strings.isNullOrEmpty(item.getShareimg())) {
                PictureUtil.yasuo(shareimghd,520,416);
                ngoods.setShareimg(iscdn ? shareimghd.replace(Constants.PATH_IMAGES + "0cdn/", rootsitecdn) : shareimghd.replace(Constants.PATH_IMAGES, rootsite));
            }
            ngoods.setId(item.getId());
            ngoods.updateById();

            AkkaServer.system.actorOf(Props.create(ResourceActor.class))
                    .tell("Goods@" + item.getId() + "@update", ActorRef.noSender());
        }

        if (!Strings.isNullOrEmpty(item.getPhotos())) {
            String[] photos = iscdn ? item.getPhotos().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getPhotos().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }

        if (!Strings.isNullOrEmpty(item.getInfo())) {
            String[] photos = iscdn ? item.getInfo().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getInfo().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }
        if (!Strings.isNullOrEmpty(item.getPackinglist())) {
            String[] photos = iscdn ? item.getPackinglist().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getPackinglist().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }
        if (!Strings.isNullOrEmpty(item.getAfterservice())) {
            String[] photos = iscdn ? item.getAfterservice().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getAfterservice().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }
        if (!Strings.isNullOrEmpty(item.getIntroduction())) {
            String[] photos = iscdn ? item.getIntroduction().replace(rootsitecdn, Constants.PATH_IMAGES + "0cdn/").split(";")
                    : item.getIntroduction().replace(rootsite, Constants.PATH_IMAGES).split(";");
            for (String img : photos) {
                PictureUtil.yasuo(img, null, null);
            }
        }
    }


}
